home *** CD-ROM | disk | FTP | other *** search
/ Disc to the Future 2 / Disc to the Future Part II Programmer's Reference (Wayzata Technology)(6013)(1992).bin / MAC / THINKC / 5 / MACVOGL- / EXAMPLES / BALLS.C next >
C/C++ Source or Header  |  1992-07-19  |  2KB  |  165 lines

  1. #include <stdio.h>
  2.  
  3. #ifdef SGI
  4. #include "gl.h"
  5. #include "device.h"
  6. #else
  7. #include "vogl.h"
  8. #include "vodevice.h"
  9. #endif
  10.  
  11. #ifndef PI
  12. #define PI 3.1415926535
  13. #endif
  14.  
  15. #ifndef TC
  16. #include <math.h>
  17. #else
  18. extern double    sin(), cos();
  19. #endif
  20.  
  21. #define RADIUS 10.0
  22. #define    SPHERE    1L
  23.  
  24. /*
  25.  * makesphere
  26.  *
  27.  *    make a sphere object
  28.  */
  29. void
  30. makesphere()
  31. {
  32.     float    r, z;
  33.     int    i, a;
  34.  
  35.     makeobj(SPHERE);
  36.  
  37.         /*
  38.          * create the latitudinal rings
  39.          */
  40.         for (i = 0; i < 1800; i += 200) {
  41.             pushmatrix();
  42.                 rotate(i, 'y');
  43.                 circ(0.0, 0.0, RADIUS);
  44.             popmatrix();
  45.         }
  46.         
  47.         /*
  48.          * create the longitudinal rings
  49.          */
  50.         pushmatrix();
  51.             rotate(900, 'x');
  52.             for (a = -900; a < 900; a += 200) {
  53.                 r = RADIUS * cos((double)a * PI / 180.0);
  54.                 z = RADIUS * sin((double)a * PI / 180.0);
  55.                 pushmatrix();
  56.                     translate(0.0, 0.0, -z);
  57.                     circ(0.0, 0.0, r);
  58.                 popmatrix();    
  59.             }
  60.         popmatrix();
  61.  
  62.     closeobj();
  63. }
  64.  
  65. /*
  66.  * a demonstration of objects
  67.  */
  68. main()
  69. {
  70.     short    val;
  71.  
  72.     winopen("balls");
  73.  
  74.     unqdevice(INPUTCHANGE);
  75.     qdevice(KEYBD);
  76.  
  77.     /*
  78.      * set up our viewing transformation
  79.      */
  80.     perspective(900, 1.0, 0.001, 500.0);
  81.     lookat(13.0, 13.0, 8.0, 0.0, 0.0, 0.0, 0);
  82.  
  83.     color(BLACK);
  84.     clear();
  85.  
  86.     /*
  87.      * Call a routine to make the sphere object
  88.      */
  89.     makesphere();
  90.  
  91.     /*
  92.      * Now draw the sphere object scaled down. We use the pushmatrix
  93.      * and the popmatrix to preserve the transformation matrix so
  94.      * that only this sphere is drawn scaled.
  95.      */
  96.     color(CYAN);
  97.  
  98.     pushmatrix();
  99.         scale(0.5, 0.5, 0.5);
  100.         callobj(SPHERE);
  101.     popmatrix();
  102.  
  103.     /*
  104.      * now we draw the same sphere translated, with a different
  105.      * scale and color.
  106.      */
  107.  
  108.     color(WHITE);
  109.  
  110.     pushmatrix();
  111.         translate(0.0, -1.4 * RADIUS, 1.4 * RADIUS);
  112.         scale(0.3, 0.3, 0.3);
  113.         callobj(SPHERE);
  114.     popmatrix();
  115.  
  116.     /*
  117.      * and maybe a few more times....
  118.      */
  119.  
  120.  
  121.     color(RED);
  122.  
  123.     pushmatrix();
  124.         translate(0.0, RADIUS, 0.7 * RADIUS);
  125.         scale(0.2, 0.2, 0.2);
  126.         callobj(SPHERE);
  127.     popmatrix();
  128.  
  129.     color(GREEN);
  130.  
  131.     pushmatrix();
  132.         translate(0.0, 1.5 * RADIUS, -RADIUS);
  133.         scale(0.15, 0.15, 0.15);
  134.         callobj(SPHERE);
  135.     popmatrix();
  136.  
  137.     color(YELLOW);
  138.  
  139.     pushmatrix();
  140.         translate(0.0, -RADIUS, -RADIUS);
  141.         scale(0.12, 0.12, 0.12);
  142.         callobj(SPHERE);
  143.     popmatrix();
  144.  
  145.     color(BLUE);
  146.  
  147.     pushmatrix();
  148.         translate(0.0, -2.0*RADIUS, -RADIUS);
  149.         scale(0.3, 0.3, 0.3);
  150.         callobj(SPHERE);
  151.     popmatrix();
  152.  
  153.     hfont("times.rb");
  154.     ortho2(0.0, 1.0, 0.0, 1.0);
  155.     hcentertext(1);
  156.     htextsize(0.08, 0.15);
  157.     move2(0.8, 0.5);
  158.     htextang(-90.0);
  159.     hcharstr("I'm very ordinary!");
  160.  
  161.     qread(&val);
  162.  
  163.     gexit();
  164. }
  165.